/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.legendshop.dao.support.PageSupport;
import com.legendshop.framework.controller.BaseController;
import com.legendshop.oa.constants.DailyLogEnum;
import com.legendshop.oa.constants.PathResolver;
import com.legendshop.oa.dto.DailyLogEntity;
import com.legendshop.oa.dto.DailyLogQueryDto;
import com.legendshop.oa.model.AdminUser;
import com.legendshop.oa.model.DailyLog;
import com.legendshop.oa.model.TaskEstimate;
import com.legendshop.oa.pages.AdminTilesPage;
import com.legendshop.oa.pages.BackPage;
import com.legendshop.oa.security.UserDetail;
import com.legendshop.oa.security.UserManager;
import com.legendshop.oa.service.DailyLogService;
import com.legendshop.oa.service.TaskEstimateService;
import com.legendshop.oa.util.PageSupportHelper;
import com.legendshop.permission.service.AdminUserService;
import com.legendshop.util.AppUtils;
import com.legendshop.util.DateUtil;
import com.legendshop.util.LogDateUtil;

/**
 * The Class DailyLogController 日志表控制器
 */
@Controller
@RequestMapping("/admin/dailylog")
public class DailyLogController extends BaseController {

	/** The log. */
	private final static Logger log = LoggerFactory.getLogger(DailyLogController.class);

	@Autowired
	private DailyLogService dailyLogService;
	
	@Autowired
	private AdminUserService adminUserService;
	
	@Autowired
	private TaskEstimateService taskEstimateService;

	/**
	 * 日志表列表查询
	 */
	@RequestMapping("/query")
	public String query(HttpServletRequest request, HttpServletResponse response) {
		
		//当前日
		String date = DateUtil.DateToString(new Date(), "yyyy-MM-dd");
		request.setAttribute("date", date);
		return PathResolver.getPath(request, response, AdminTilesPage.DAILYLOG_LIST_PAGE);

	}
	
	/**
	 * 周的形式日志表列表查询
	 */
	@RequestMapping("/queryContent")
	public String queryContent(HttpServletRequest request, HttpServletResponse response, DailyLogQueryDto dailyLogQueryDto) {
		if(AppUtils.isNotBlank(dailyLogQueryDto.getUserName())){
			AdminUser adminUser = adminUserService.getAdminUserByUserName(dailyLogQueryDto.getUserName());
			if(adminUser != null){
				request.setAttribute("userRealName", adminUser.getRealName());
			}
		}
		
		//当前日
		String date = DateUtil.DateToString(new Date(), "yyyy-MM-dd");
		request.setAttribute("date", date);
		
		//查询当日的日志
		return PathResolver.getPath(request, response, BackPage.DAILYLOG_PAGE);

	}

	/**
	 * 移动日期控件的内容，更换时间
	 */
	@RequestMapping("/moveTaskEstimate")
	@ResponseBody
	public String moveTaskEstimate(HttpServletRequest request,Long id, String date) {

		UserDetail ud = UserManager.getUser(request);
		if(ud == null){
			return "failed";
		}
		String userName = ud.getUsername();

		boolean result = dailyLogService.moveTaskEstimate(id, userName, date);
		if(result){
			//返回成功
			return "success";
		}else{
			//返回失败
			return "failed";
		}

	}

	
	/**
	 * 列表形式日志表列表查询
	 */
	@RequestMapping("/queryLogList")
	public String queryLogList(HttpServletRequest request, HttpServletResponse response, DailyLogQueryDto dailyLogQueryDto) {
		if(AppUtils.isNotBlank(dailyLogQueryDto.getUserName())){
			AdminUser adminUser = adminUserService.getAdminUserByUserName(dailyLogQueryDto.getUserName());
			if(adminUser != null){
				request.setAttribute("userRealName", adminUser.getRealName());
			}
		}
		
		Integer type = dailyLogQueryDto.getDailyLogEnum();
		
		Date startDate = null;
		Date endDate = null;
		Date now = new Date();
		if(DailyLogEnum.TODAY.value().equals(type)){
			startDate = LogDateUtil.parseOffsetDate(now,0);
			endDate = LogDateUtil.parseOffsetDate(now,1);
		}else if(DailyLogEnum.YESTERDAY.value().equals(type)){
			startDate = LogDateUtil.parseOffsetDate(now,-1);
			endDate = LogDateUtil.parseOffsetDate(now,0);
		}else if(DailyLogEnum.THIS_WEEK.value().equals(type)){
			startDate = LogDateUtil.getMondayOfThisWeek();
			endDate = LogDateUtil.getSundayOfThisWeek();
		}else if(DailyLogEnum.LAST_WEEK.value().equals(type)){
			startDate = LogDateUtil.getMondayOfLastWeek();
			endDate = LogDateUtil.getSundayOfLastWeek();
		}else if(DailyLogEnum.THIS_MONTH.value().equals(type)){
			startDate = LogDateUtil.getFirstDayOfThisMonth();
			endDate = LogDateUtil.getLastDayOfThisMonth();
		}else if(DailyLogEnum.LAST_MONTH.value().equals(type)){
			startDate = LogDateUtil.getFirstDayOfLastMonth();
			endDate = LogDateUtil.getLastDayOfLastMonth();
		}
		
//		if(!UserManager.hasFunction(request, "F_AUDIT_DATA")){
//			UserDetail ud = UserManager.getUser(request);
//			dailyLogQueryDto.setUserName(ud.getUsername());
//		}
		
		PageSupport<TaskEstimate> ps = taskEstimateService.getTaskEstimateByDay(startDate,endDate, dailyLogQueryDto);
		PageSupportHelper.savePage(request, ps);
		
		return PathResolver.getPath(request, response, BackPage.DAILYLOG_LIST_PAGE);

	}
	

	

	/**
	 * 每日的工时
	 * date: 日期
	 */
	@RequestMapping("/queryTaskEstimate")
	@ResponseBody
	public List<DailyLogEntity> taskEstimateByDay(HttpServletRequest request, HttpServletResponse response, String startDate, String endDate, String userName) {
		Date start = DateUtil.StringToDate(startDate,"yyyy-MM-dd");
		Date end = DateUtil.StringToDate(endDate,"yyyy-MM-dd");
		String userName1;
		if(AppUtils.isNotBlank(userName)){
			userName1 = userName;
		}else{
			UserDetail ud = UserManager.getUser(request);
			userName1 = ud.getUsername();
		}
		
		List<DailyLogEntity> dailyLogEntity = dailyLogService.queryDailyLog(start, end, userName1);
		return dailyLogEntity;
	}

	/**
	 * 加载日志编辑页面
	 */
	@RequestMapping(value = "/load/{dateStr}")
	public String load(HttpServletRequest request, HttpServletResponse response, @PathVariable String dateStr) {
		Date date = null;
		try {
			date = DateUtil.StringToDate(dateStr, "yyyyMMdd");// 尝试转换日期
		} catch (Exception e) {
			log.error("转换日期错误", e);
			return null;
		}
		request.setAttribute("dataFormat", DateUtil.DateToString(date, "yyyy-MM-dd"));
		return PathResolver.getPath(request, response, AdminTilesPage.DAILYLOG_EDIT_PAGE);
	}

	/**
	 * 加载今日日志编辑页面
	 */
	@RequestMapping(value = "/load")
	public String load(HttpServletRequest request, HttpServletResponse response) {
		Date date = new Date();
		request.setAttribute("dataFormat", DateUtil.DateToString(date, "yyyy-MM-dd"));
		return PathResolver.getPath(request, response, AdminTilesPage.DAILYLOG_EDIT_PAGE);
	}

	/**
	 * 更新编辑页面
	 */
	@RequestMapping(value = "/update")
	public String update(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id) {
		DailyLog dailyLog = dailyLogService.getDailyLog(id);
		// String result = checkPrivilege(request,
		// UserManager.getUsername(request.getSession()), dailyLog.getUserName());
		// if(result!=null){
		// return result;
		// }
		request.setAttribute("dailyLog", dailyLog);
		return "forward:/admin/dailyLog/query.htm";
		// TODO, replace by next line, need to predefined BackPage parameter
		// return PathResolver.getPath(request, response, BackPage.DAILYLOG_EDIT_PAGE);
	}

}
